package cn.com.assistant.controller;

import cn.com.assistant.annotation.AdminLoginToken;
import cn.com.assistant.entity.DataEntity;
import cn.com.assistant.entity.SchemaEntity;
import cn.com.assistant.entity.TableEntity;
import cn.com.assistant.mapper.DataMapper;
import cn.com.assistant.mapper.TableMapper;
import cn.com.assistant.service.DataService;
import cn.com.assistant.utils.RequestUtil;
import cn.com.assistant.utils.ResponseMessage;
import cn.com.assistant.utils.UUIDUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/data")
public class DataController {
 
    @Autowired
    DataService dataService;
    @Autowired
    DataMapper dataMapper;
    @Autowired
    TableMapper tableMapper;

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PostMapping("/insert")
    @ApiOperation(value = "添加实体数据", notes = "添加实体数据")
    @ApiImplicitParam(name = "dataEntity", value = "属性", required = true, dataType = "DataEntity")
    public ResponseMessage insert(@RequestBody @Valid DataEntity dataEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        dataEntity.setId(UUIDUtil.getUUID());
        dataEntity.setCreatedDate(new Date());
        // 存储关联实体信息
        TableEntity tableEntity = tableMapper.selectById(dataEntity.getPrincipalId());
        dataEntity.setPrincipalProperty(tableEntity.getChineseName());
        boolean flag = dataService.save(dataEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("保存成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("保存失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @PutMapping("/update")
    @ApiOperation(value = "修改实体数据", notes = "修改实体数据")
    @ApiImplicitParam(name = "dataEntity", value = "实体数据", required = true, dataType = "DataEntity")
    public ResponseMessage update(@RequestBody @Valid DataEntity dataEntity, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.validFailResponse().setMsg(bindingResult.getAllErrors());
        }
        dataEntity.setUpdatedDate(new Date());
        boolean flag = dataService.updateById(dataEntity);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("修改成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("修改失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除实体数据", notes = "删除实体数据")
    @ApiImplicitParam(name = "实体数据id", value = "实体数据id")
    public ResponseMessage delete(@RequestParam("id") String id) {

        boolean flag = dataService.removeById(id);
        if (flag) {
            return ResponseMessage.defaultResponse().setMsg("删除成功！");
        } else {
            return ResponseMessage.failResponse().setMsg("删除失败！");
        }
    }

    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @GetMapping("/findById")
    @ApiOperation(value = "查询实体数据", notes = "查询实体数据")
    @ApiImplicitParam(name = "实体数据id", value = "实体数据id")
    public ResponseMessage findById(@RequestParam("id") String id) {
        DataEntity entity = dataService.getById(id);
        return ResponseMessage.defaultResponse().setData(entity);
    }


//    @AdminLoginToken
    @CrossOrigin(origins = "*",maxAge = 3600)
    @ApiOperation(value = "实体数据分页列表", notes = "实体数据分页列表")
    @GetMapping(value = "/page")
    public ResponseMessage findByPage(@RequestParam(value = "page", defaultValue = "0") Integer pageNum,
                                      @RequestParam(value = "size", defaultValue = "10") Integer size,
                                      HttpServletRequest request) {
        Map<String, Object> filters = RequestUtil.getParameters(request);
        LambdaQueryWrapper<DataEntity> dataLambdaQueryWrapper = Wrappers.lambdaQuery();
        Page<DataEntity> dataPage = new Page<>(pageNum , size , true);
        IPage<DataEntity> dataIPage = dataMapper.selectPage(dataPage , dataLambdaQueryWrapper);
        return ResponseMessage.defaultResponse().setData(dataIPage);
    }

}